package com.pzb.web.controller.wechat;

import java.util.Date;
import java.util.List;
import java.util.Map;

import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.wechat.domain.WechatShoppingCart;
import com.pzb.wechat.service.IWechatShoppingCartService;

/**
 * 购物车Controller
 * 
 * @author Mr_Wu
 * @date 2022-01-19
 */
@RestController
@RequestMapping("/cart")
public class WechatShoppingCartController extends BaseController
{
    @Autowired
    private IWechatShoppingCartService wechatShoppingCartService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam String extAppid)
    {
        List<WechatShoppingCart> list = wechatShoppingCartService.selectWechatShoppingCartList(getOpenid(),extAppid);
        AjaxResult result = AjaxResult.success(list);
        Integer cartTotal = wechatShoppingCartService.selectShoppingCartTotal(getOpenid(),extAppid);
        result.put("cartTotal",cartTotal);
        return result;
    }

    /**
     * 新增购物车
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Map param)
    {
        String goodsId = param.get("goodsId").toString();
        String goodsUnit = (String) param.get("goodsUnit");
        Integer goodsNum = (Integer) param.get("goodsNum");
        String extAppid = (String) param.get("extAppid");
        //查询原购物车有无
        WechatShoppingCart cart = wechatShoppingCartService.selectWechatShoppingCart(getOpenid(),extAppid,Long.parseLong(goodsId),goodsUnit);
        int count = 0;
        if(cart!=null){
            cart.setNumber(cart.getNumber()+goodsNum);
            count = wechatShoppingCartService.updateWechatShoppingCart(cart);
        }else {
            cart=new WechatShoppingCart();
            cart.setCreateTime(new Date());
            cart.setExtAppid(extAppid);
            cart.setOpenid(getOpenid());
            cart.setGoodsId(Long.parseLong(goodsId));
            cart.setGoodsUnit(goodsUnit);
            cart.setNumber(goodsNum);
            count = wechatShoppingCartService.insertWechatShoppingCart(cart);
        }
        Integer cartTotal = wechatShoppingCartService.selectShoppingCartTotal(getOpenid(),extAppid);
        AjaxResult result =toAjax(count);
        result.put("cartTotal",cartTotal);
        return result;
    }

    /**
     * 修改购物车
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody WechatShoppingCart shoppingCart)
    {
        AjaxResult result = toAjax(wechatShoppingCartService.updateWechatShoppingCart(shoppingCart));
        Integer cartTotal = wechatShoppingCartService.selectShoppingCartTotal(getOpenid(),shoppingCart.getExtAppid());
        result.put("cartTotal",cartTotal);
        return result;
    }

    /**
     * 删除购物车
     */
	@PostMapping("/clear")
    public AjaxResult clear(@RequestBody Map param)
    {

        List<Long> cartIds = (List<Long>) param.get("cartIds");
        return toAjax(wechatShoppingCartService.deleteWechatShoppingCartByIds(cartIds));
    }
}
